package com.msb.servicedriveuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.commons.constant.CommonStatusEnum;
import com.msb.commons.constant.DriverCarConstant;
import com.msb.commons.constant.DriverUserExistsConstant;
import com.msb.commons.dto.Car;
import com.msb.commons.dto.DriverCarBindingRelationship;
import com.msb.commons.dto.DriverUser;
import com.msb.commons.dto.DriverUserWorkStatus;
import com.msb.commons.response.DriverUserExistsResponse;
import com.msb.commons.response.OrderDriverResponse;
import com.msb.commons.response.ResponseResult;
import com.msb.servicedriveuser.mapper.CarMapper;
import com.msb.servicedriveuser.mapper.DriverCarBindingRelationshipMapper;
import com.msb.servicedriveuser.mapper.DriverUserMapper;
import com.msb.servicedriveuser.mapper.DriverUserWorkStatusMapper;
import com.msb.servicedriveuser.service.DriverUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import sun.awt.windows.WLightweightFramePeer;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chshh
 * @since 2022-10-25
 */
@Service
public class DriverUserServiceImpl extends ServiceImpl<DriverUserMapper, DriverUser> implements DriverUserService {

    @Resource
    private DriverUserMapper driverUserMapper;

    @Resource
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Resource
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Resource
    private CarMapper carMapper;

    @Override
    public ResponseResult addDriverUser(DriverUser driverUser){
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);
        driverUserMapper.insert(driverUser);
        //初始化司机工作状态表
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarConstant.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<DriverUser> getDriverUserByPhone(String driverPhone) {
        QueryWrapper<DriverUser> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_phone",driverPhone)
                .eq("state", DriverCarConstant.DRIVER_STATE_YES);
        List<DriverUser> driverUsers = driverUserMapper.selectList(wrapper);
        DriverUserExistsResponse response = new DriverUserExistsResponse();
        if(driverUsers.size()==0){
            response.setDriverPhone(DriverUserExistsConstant.DRIVER_NO);
            response.setIfExists(DriverUserExistsConstant.IFEXISTS_NO);
            return ResponseResult.fail(response);
        }

        response.setDriverPhone(DriverUserExistsConstant.DRIVER_YES);
        response.setIfExists(DriverUserExistsConstant.IFEXISTS_YES);

        return ResponseResult.success(response);

    }

    @Override
    public ResponseResult<Boolean> isAvailableDriver(String cityCode) {
        int i = driverUserMapper.selectDriverUserCountByCityCode(cityCode);
        if (i>0) {
            return ResponseResult.success(true);
        }else {
            return ResponseResult.success(false);
        }

    }

    @Override
    public ResponseResult getAvailableDriver(Long carId) {
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.eq("car_id",carId)
                .eq("bind_state",DriverCarConstant.DRIVER_CAR_BIND);
        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipMapper.selectList(wrapper);

        if (driverCarBindingRelationships.size()>0){
            DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationships.get(0);
            Long driverId = driverCarBindingRelationship.getDriverId();

            QueryWrapper<DriverUserWorkStatus> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("driver_id",driverId)
                    .eq("work_status",DriverCarConstant.DRIVER_WORK_STATUS_START);
            DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(wrapper1);

            if (null == driverUserWorkStatus){
                return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
            }else {
                //查询司机信息
                QueryWrapper<DriverUser> driverUserWrapper = new QueryWrapper<>();
                driverUserWrapper.eq("id",driverId);
                List<DriverUser> driverUsers = driverUserMapper.selectList(driverUserWrapper);
                //查询车辆信息
                QueryWrapper<Car> carWrapper = new QueryWrapper<>();
                carWrapper.eq("id",carId);
                Car car = carMapper.selectOne(carWrapper);


                if (driverUsers.size()>0){
                    DriverUser driverUser = driverUsers.get(0);
                    OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
                    orderDriverResponse.setCarId(carId);
                    orderDriverResponse.setDriverId(driverId);
                    orderDriverResponse.setDriverPhone(driverUser.getDriverPhone());

                    orderDriverResponse.setLicenseId(driverUser.getLicenseId());
                    orderDriverResponse.setVehicleNo(car.getVehicleNo());
                    return ResponseResult.success(orderDriverResponse);
                }else {
                    return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
                }
            }
        }else {
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
        }
    }

    @Override
    public ResponseResult updateDriverUser(DriverUser driverUser){
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtModified(now);
        driverUserMapper.updateById(driverUser);
        return ResponseResult.success();
    }

}
